/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.pass;

import org.llama.jmex.task.TaskRunner;
import org.llama.jmex.terra.SimpleTerraData;
import org.llama.jmex.terra.TerraData;
import org.llama.jmex.terra.TerraManager;
import org.llama.jmex.terra.TerraMesh;
import org.llama.jmex.terra.TerraView;
import org.llama.jmex.terra.TerraViewProperties;
import org.llama.jmex.terra.TerraViewTask;
import org.llama.jmex.terra.XYKey;

import com.jme.math.Vector3f;

public class SimpleDistanceGeomPass extends DistancePass {

	private TaskRunner runner;

	public SimpleDistanceGeomPass(int distance, TaskRunner runner) {
		super(distance, TerraManager.K_TD, true);
		this.runner = runner;
	}
	
	public void remove(XYKey xy, TerraView tv) {
//System.out.println("geomPass.remove: "+xy);
		
		Object value = tv.getTerraManager().get(xy);
		tv.getTerraManager().remove(xy);
		if (value != null) {
			TerraData tdata = (TerraData) value;
			synchronized(tv.getTerraNodes()) {
				TerraMesh mesh = tdata.getTerraMesh();
				tdata.deleted = true;
				if (mesh != null) {
					tdata.cleanUp();
					System.out.println("Detach: "+mesh.id);
					while (tv.detachChild(mesh) != -1);
					mesh.cleanUp();
				}
				while (tv.getTerraNodes().remove(value));
			}
		}

//System.out.println("geomPass.removed: "+xy);
	}
	
	public TerraData createTerraData(TerraView tv, XYKey xy) {
		TerraManager tm = tv.getTerraManager();
		TerraViewProperties prop= tv.getTerraProperties();

		int mapx = tm.getBlockTopPosition(xy.x);
		int mapy = tm.getBlockTopPosition(xy.y);

		return new SimpleTerraData(tm, mapx, mapy, prop, new Vector3f(tm.getBlockTopPosition(xy.x)
				* prop.terraScale.x, 0, tm.getBlockTopPosition(xy.y) * prop.terraScale.z), xy);
	}

	public void add(XYKey xy, final TerraView tv) {
//System.out.println("geomPass.add: "+xy);
		final TerraManager tm = tv.getTerraManager();
		if (tm.getTerraData(xy.x, xy.y) != null)
			return;

		if (!((tm
				.getMapAsByteBuffer(tm.findMap(tm.getBlockTopPosition(xy.x)), tm.findMap(tm.getBlockTopPosition(xy.y))) != null)
				&&

				(tm.getMapAsByteBuffer(tm.findMap(tm.getBlockBottomPosition(xy.x)), tm.findMap(tm
						.getBlockTopPosition(xy.y))) != null)
				&&

				(tm.getMapAsByteBuffer(tm.findMap(tm.getBlockBottomPosition(xy.x)), tm.findMap(tm
						.getBlockBottomPosition(xy.y))) != null) &&

		(tm.getMapAsByteBuffer(tm.findMap(tm.getBlockTopPosition(xy.x)), tm.findMap(tm.getBlockBottomPosition(xy.y))) != null)

		)) {
			return;
		}
		
		xy.update(xy.x, xy.y, TerraManager.K_TD);
		if (tv.getTaskManager().containsTask(xy))
			return;
		xy = new XYKey(xy);
		
		TerraViewTask task = new TerraViewTask(tv, xy) {

			public boolean runTask() {

				try {
					long count = System.currentTimeMillis();
					TerraData tb = createTerraData(tv, xy);
					tm.addTerraData(tb, xy.x, xy.y);
					synchronized (tv.getTerraNodes()) {
						tb.xx = xy.x;
						tb.yy = xy.y;
						tv.getTerraNodes().add(tb);

					}
					tv.add(xy);
//					System.out.println("Terra Block took:"
//							+ (System.currentTimeMillis() - count) + " " + xy.x + ":" + xy.y);
					tv.updateNextFrame();

				} catch (Exception e) {
					e.printStackTrace();
					// return false;
				}

				return true;
			}

			public String toString() {
				return new String("Task: " + xy + " " + priority);
			}

		};
		tv.getTaskManager().addTask(task, runner);
//System.out.println("geomPass.added: "+xy);

	}

	

}
